package org.geektimes.projects.user.validator.bean.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @author Sanhong
 */
public class PhoneNumberValidator  implements ConstraintValidator<IsPhoneNumber, Object> {

    private static Pattern pattern = Pattern.compile("^[1]\\d{10}$");

    @Override
    public void initialize(IsPhoneNumber constraintAnnotation) {

    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        return pattern.matcher(value.toString()).matches();
    }
}
